package com.zuorenke.vertx.handler;

import com.zuorenke.help.HandlerUtils;
import com.zuorenke.model.constants.Constants;
import com.zuorenke.model.Result;
import com.zuorenke.vertx.anno.Order;
import com.zuorenke.vertx.standard.PostHandler;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-10 15:47:13
 **/
@Component
@Order(900)
@Slf4j
public class ResponseBuildResultPostHandler implements PostHandler<RoutingContext> {
	@Override
	public void handle(RoutingContext event) {
		Object data = event.get(Constants.WEB_DATA);
		Result result = new Result();
		if (data instanceof Result) {
			result = (Result) data;
		} else {
			Integer code = event.get(Constants.WEB_CODE);
			if (code != null) {
				result.setCode(code);
				result.setMsg(event.get(Constants.WEB_MSG));
				result.setSuccess(false);
			} else {
				result.setData(event.get(Constants.WEB_DATA));
			}
		}
		event.put(Constants.WEB_RESULT, result);
		HandlerUtils.next(event);
	}
}
